<template>
  <div>
    <el-button type="primary" @click="openCreateUpdateDialog(null)">
      创建角色
    </el-button>

    <el-table :data="tableList" border>
      <el-table-column fixed label="ID" prop="id" min-width="50"/>
      <el-table-column label="角色名称" prop="name" min-width="100"/>
      <el-table-column label="角色说明" prop="comment" min-width="200"/>
      <el-table-column label="创建时间" prop="createTime" min-width="160">
        <template v-slot="scope">
          {{ scope.row.createTime|transformDateFormat }}
        </template>
      </el-table-column>
      <el-table-column label="更新时间" prop="updateTime" min-width="160">
        <template v-slot="scope">
          {{ scope.row.updateTime|transformDateFormat }}
        </template>
      </el-table-column>
      <el-table-column label="操作" fixed="right" width="100">
        <template v-slot="scope">
          <el-button type="text" size="small" @click="deleteRole(scope.row.id)">删除</el-button>
          <el-button type="text" size="small" @click="openCreateUpdateDialog(scope.row.id)">编辑</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination :current-page="pageForm.current" :total="pageForm.total" :page-size="pageForm.size"
                   :page-sizes="pageForm.sizes"
                   :style="pageForm.style" :layout="pageForm.layout"
                   @current-change="page" @size-change="changeSize"/>

    <create-update-dialog :id="currentId" :visible="createUpdateDialogVisible"/>
  </div>
</template>

<script>
import {_delete, page} from "@/api/system/role";
import {createPageRequestBody} from "@/api/system/pagination";
import CreateUpdateDialog from "@/views/system/role/createUpdateDialog";
import BdpPagination from "@/views/common/bdpPagination.vue";

export default {
  name: "Index",
  components: {BdpPagination, CreateUpdateDialog},
  data() {
    return {
      tableForm: {},
      pageForm: createPageRequestBody(),
      tableList: [],
      createUpdateDialogVisible: false,
      currentId: null
    }
  },

  mounted() {
    this.page()
  },

  methods: {
    openCreateUpdateDialog(id) {
      this.createUpdateDialogVisible = true
      this.currentId = id
    },

    page(current = 1) {
      this.pageForm.current = current
      page(this.tableForm, this.pageForm.current, this.pageForm.size).then(res => {
        this.tableList = res.data.records
        this.pageForm.total = res.data.total
      })
    },
    changeSize(size) {
      this.pageForm.size = size
      this.page(1)
    },

    deleteRole(id) {
      this.$confirm('确定删除？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        _delete(id).then(() => {
          this.$message({type: 'success', message: '删除成功'})
          this.page()
        })
      }).catch(() => {
        this.$message({type: 'info', message: '删除取消'})
      })
    }
  }
}
</script>

<style scoped>

</style>
